package com.singleton.pattern.singleton.sabotage;

import com.singleton.pattern.singleton.demo.ContainerSingleton;
import com.singleton.pattern.singleton.demo.HungrySingleton;
import com.singleton.pattern.singleton.demo.TestMain;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;

/**
 *  破坏单例
 */
public class Sabottage {
    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException, ClassNotFoundException {
        ContainerSingleton s1 = null;
        ContainerSingleton s2 = (ContainerSingleton) ContainerSingleton.getBean("com.singleton.pattern.singleton.demo.ContainerSingleton");

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("TestMain.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(s2);
            oos.flush();
            oos.close();


            FileInputStream fis = new FileInputStream("TestMain.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);
            s1 = (ContainerSingleton)ois.readObject();
            ois.close();

            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s1 == s2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
